var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;
var valide = libs.formValide;

var _page = {}


function updateUserInfo(id, v){
    $('#'+id).val(v)
    return updateUserInfo
}

// 填充用户数据
function _fun(data){
    console.log(data);
    var _add = data.info.address
    var _addinfo = data.info.currentCommunity || ''
    updateUserInfo('uc_edit_name', (data.info.userName||''))
                  ('uc_edit_address', ( _add ? (data.info.address.address||'') : '') )
                  ('uc_edit_zone', (_addinfo.cityName + _addinfo.districtName + _addinfo.communityName||''))
}

function checkName(stat, reg){
    if (!stat){
        reg.tips('请填写用户名')
        return false;
    }
    else {
        if (libs.strLen(this.value)>2){
            return true;
        }
        else {
            reg.tips('用户名长度必须大于2')
            return false;
        }
    }
}

var bindEvent = function(){
    var _user = SA.get('USER')
    if (!_user.login){
        SA.set('USER', _fun)
    }
    else {
        var _user = SA.get('USER')
        _fun(_user)
    }

    var check_uc_edit = valide('Form_uc_edit_user_info')
    ('uc_edit_name', 'username', checkName)
    ('uc_edit_zone', 'noop')
    ("uc_edit_address", 'noop')

    function _valide(){
        check_uc_edit(function(query,eles){
            if (query.ckstat){
                query.method = 'get'
                query.districtName = _user.info.currentCommunity.communityName
                query.communityId = _user.info.currentCommunity.communityId
                query.dscd = _user.info.currentCommunity.districtId
                query.address = query.uc_edit_address
                query.contact = query.uc_edit_name
                query.contactPhone = _user.info.mobile
                query.login = _user.login

                delete query.ckstat
                delete query.uc_edit_address
                delete query.uc_edit_name
                delete query.uc_edit_zone

                var postdata = {
                    login: _user.login,
                    userName: query.contact,
                    method: 'get'
                }
                api.req('/createUserAddress', query, function(data, status, xhr){
                    if (!data.code){
                        var _pre = router.pre();
                        if (_pre.hash!=='uc/location')
                            router.goback()
                    }
                })
            }
            else{
                // $.each(eles, function(item){
                //     console.log(item);
                //     setTimeout(function(){
                //         // ....
                //     },300)
                // })
                query.contact = query.uc_edit_name
                var postdata = {
                    login: _user.login,
                    userName: query.contact,
                    method: 'get'
                }
                api.req('/createUserAddress', query, function(data, status, xhr){
                    if (!data.code){
                        var _pre = router.pre();
                        if (_pre.hash!=='uc/location')
                            router.goback()
                    }
                })
            }
        })
    }

    $('#uc_edit_zone').click(function(){
        router('uc/location', {back: 'uc/edit'})
    })

    $('#uc_edit_jump').click(function(){
        router('uc/index')
    })

    $('.uc_edit_submit').click(function(){
        _valide()
    })

}

function renderHtml(){
    var me = this;
    var oUser = SA.get('USER');
    api.req('/userInfo', {login: oUser.login, method: 'get'}, function(ddd){
        var _resault = {login: oUser.login, info: ddd}
        SA.set('USER', _resault)
        me.main()
    })
}

//演示模块
var Show = React.createClass({
    mixins: [ItemMixin],
    componentWillMount: function() {

    },
    render:function(){
        return (
            <div className="default_div app_index">
                <ul className="default_Picture">
                    <li className="dP_pic">
                        <label>头像</label>
                        <div className="dp_pic_r">
                            <img src="./images/ic_default_head.png"/>
                            {/*<i className="iconfont icon-xiangyou"></i>*/}
                        </div>
                    </li>
                    <li>
                        <label>姓名</label>
                        <input id="uc_edit_name" type="text" defaultValue="" placeholder="请输入姓名"/>
                        {/*<i className="iconfont icon-xiangyou"></i>*/}
                    </li>
                    <li>
                        <label>所在小区</label>
                    <input id="uc_edit_zone" type="text" defaultValue="" readOnly="readOnly"/>
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                    <li>
                        <label>具体地址</label>
                        <input id="uc_edit_address" type="text" defaultValue="" placeholder="请输入具体到门牌号的地址"/>
                        {/*<i className="iconfont icon-xiangyou"></i>*/}
                    </li>
                    <li>
                        {/*<label>具体地址</label>*/}
                        <input id="uc_edit_jump" type="text" defaultValue="跳过" readOnly="readOnly" style={{color:'#ccc'}}/>
                        {/*<i className="iconfont icon-xiangyou"></i>*/}
                    </li>
                </ul>
                <div className="default_foot" style={{backgroundColor:'transparent',marginBottom:"0.2rem"}}>
                    <div className="f_submit" style={{width:'100%'}}>
                        <input type="submit" defaultValue="确定" className="f_btn uc_edit_submit" />
                    </div>
                </div>
            </div>
        );
    }
});

function start(name){
    return pages.new({
        boot:function(){

        },
        trigger:function(self, data){
            _page.community = data
            this.libs.changeTitle('地址');    //更改当前页面标题
            renderHtml.call(this)
            // this.main()
        },
        main: function(){
            this.render(
                <Show itemDefaultMethod={bindEvent} />,
                document.getElementById(name)
            )
        },
        end: function(){
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}

module.exports = start;
